
import  rsa

(publicKey,privateKey) = rsa.newkeys(1024)

with open('pubkey.pem','w') as f:
    f.write(publicKey.save_pkcs1().decode())
with open('prikey.pem' ,'w+') as f:
    f.write(privateKey.save_pkcs1().decode())

def encrypt(message):
    ciphertext = rsa.encrypt(message.encode('utf-8'),publicKey)
    return ciphertext

def decrypt(ciphertext):
    text = rsa.decrypt(bytes(ciphertext) ,privateKey)
    return text.decode()

if __name__=='__main__':
    message = input('Plaintext: ')
    ciphertext = encrypt(message)
    print('=================================')
    print('Ciphertext: \n' + str(ciphertext))
    plaintext = decrypt(ciphertext)
    print('=================================')
    print('After decryption: '+ plaintext)
    print('=================================')
